<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Email:1050187159@qq.com
 * Date: 2018/8/20
 * Time: 21:49
 */
namespace Admin\Controller;

class CollectsController extends BaseController
{
     public function index()
     {
         $type = I('get.type');
         if (empty($tpye)) {
             $where['type'] = array('neq', 0);
         } else {
             $where['type'] = array('eq', $type);
         }


         $Collect = D('Collect');

         //查询满足要求的总记录数
         $count = $Collect->count();
         //实例化分页类 传入总记录数和每页显示的记录数(10)
         $Page = new \Think\Page($count,10);
         $Page->setConfig('next',C('PAGE_CONFIG.next'));
         $Page->setConfig('prev',C('PAGE_CONFIG.prev'));
         $Page->setConfig('last',C('PAGE_CONFIG.last'));
         $Page->setConfig('first',C('PAGE_CONFIG.first'));
         $Page->lastSuffix=C('PAGE_CONFIG.lastSuffix');//在此设置一行
         $show = $Page->show();// 分页显示输出
         //进行分页数据查询 注意limit方法的参数要使用Page类的属性
         $collects = $Collect->order('collected_at desc')->limit($Page->firstRow.','.$Page->listRows)->where($where)->select();

         foreach ($collects as $key => $collect) {
             if ($collect['type'] == 1) {
               $collects[$key]['type'] = '建筑';
             }
             if ($collect['type'] == 2) {
                 $collects[$key]['type'] = '交通';
             }
             if ($collect['type'] == 3) {
                 $collects[$key]['type'] = '健康';
             }
             if (!file_exists(APP_PATH.$collect['path'])){
                 $collects[$key]['path'] =  '';
             }
         }

         $this->assign(array(
             'collects' => $collects,
             'show'     => $show
         ));
         $this->display('Collects/list');
     }

     public function download()
     {
         $id = I('get.id');
         $collect = D('Collect')->find($id);
         $file = APP_PATH.$collect['path'];
         if (file_exists($file)) {
             header("Content-type: application/octet-stream");
             header('Content-Disposition: attachment; filename="' . basename($file) . '"');
             header("Content-Length: ". filesize($file));
             readfile($file);
         } else {
             $this->_empty();
         }
     }

     public function delete()
     {
         $id = I('get.id');
         $Collect = D('Collect');
         $collect =D('Collect')->find($id);

         if(file_exists(APP_PATH.$collect['path'])) {
             unlink(APP_PATH.$collect['path']);
         }

         if ($Collect->delete($id)) {
             unlink(APP_PATH.$collect['path']);
             $this->ajaxReturn('true');
         } else {
             $this->ajaxReturn($Collect->getError());
         }
     }

     public function detail()
     {
         $id = I('get.id');
         $Collect = D('Collect');
         $collect = $Collect->find($id);

         if ($collect['type'] == 1) {
             $collect['type'] = '建筑';
         }
         if ($collect['type'] == 2) {
             $collect['type'] = '交通';
         }
         if ($collect['type'] == 3) {
             $collect['type'] = '健康';
         }
         if (!file_exists(APP_PATH.$collect['path'])){
             $collect['path'] =  '';
         }

         $this->assign(array(
             'collect'  => $collect
         ));
         $this->display();
     }
}